<?php
class ppController extends Zend_Controller_Action {
	public function indexAction() {
		$msData = new pp_msg ( );
		switch ( $this->_getParam ( 'act' )) {
			case 'insert' :
				$myMsg = $this->_getParam ( 'myMsg' );
				$addressee = $this->_getParam ( 'addressee' );
				$auth = Zend_Auth::getInstance ();
				$addresser = $auth->getIdentity ();

				$result = $msData->insert ( array ('message' => $myMsg, 'addressee' => $addressee, 'addresser' => $addresser ) );
				$this->view->data = $result;
				break;
					
			case 'login' :

				$this->userName = $this->_getParam ( "userName" );
				$this->password = $this->_getParam ( "passWord" );

				if ($this->userName == "" || $this->password == "") {

					$this->errorMsg [0] = "infoWrong";
					$this->view->data = $this->errorMsg;
					return;
				}

				if (! $this->db) {
					$this->db = Zend_Registry::get ( "db" );
				}

				$authAdpter = new Zend_Auth_Adapter_DbTable ( $this->db );

				$authAdpter->setTableName ( "pp_user" );
				$authAdpter->setIdentityColumn ( "name" );
				$authAdpter->setCredentialColumn ( "password" );
				$authAdpter->setCredentialTreatment ( "md5(?)" );
				$authAdpter->setIdentity ( $this->userName );
				$authAdpter->setCredential ( $this->password );

				$auth = Zend_Auth::getInstance ();
				$result = $auth->authenticate ( $authAdpter )->isValid ();

				if ($result) {
					$this->errorMsg [0] = "true";
				} else {
					$this->errorMsg [0] = "passWrong";
				}
				$this->view->data = $this->errorMsg;
				break;
					
			case 'getUserList' :
				{
					$db = Zend_Registry::get ( 'db' );
					$select = $db->select ();
					$select->from ( array ('u' => 'pp_user' ), array ('name' ) );
					$select->joinLeft ( array ('m' => 'pp_msg' ), 'u.name = m.addresser', array ('a' => 'count(readen)*2', 'b' => 'sum(readen)' ) );
					$select->group ( 'name' );
					$select->where ( 'addressee = ?', $this->_getParam ( 'userName' ) );
					$select->where ( 'addresser != ?', $this->_getParam ( 'userName' ) );
					$sql = $select->__toString ();
//					Zend_Debug::dump ( $sql );
					
					$sess = new session ( );
					$sessSelect = $sess->select ();
					$sessSelect->where ( 'session_data != ?', '' );
					$sessSelect->where ( 'modified > ?', time () - 1440 );
					$sessSelect->where ( 'session_data Like ?', '%Zend%' );
					$sessSelect->group ( 'session_data' );
					$sessData = $sess->fetchAll ( $sessSelect );
					foreach ( $sessData as $v ) {
						$vv = strtok ( $v->session_data, '|' );
						$vv = strtok ( '|' );
						$vv = unserialize ( $vv );
						$data [] = $vv ['storage'];
					}
					$userData = $db->fetchAll ( $select );
//					zend_debug::dump($sessData);
						
					if (is_array ( $data )) {
						foreach ( $userData as $k => $v ) {
							if ($v ['a'] != $v ['b']) {
								$userData [$k] ['m'] = 'on';
							} else {
								$userData [$k] ['m'] = 'off';
							}
							if (in_array ( $v ['name'], $data )) {
								$userData [$k] ['online'] = 'on';
							} else {
								$userData [$k] ['online'] = 'off';
							}
							unset ( $userData [$k] ['a'] );
							unset ( $userData [$k] ['b'] );
						}
					}

					$this->view->data = $userData;
//					Zend_Debug::dump ( $userData );
					break;
				}
			default :
				$auth = Zend_Auth::getInstance ();
				$addressee = $auth->getIdentity ();

				$select = $msData->select ();
				$select->where ( 'readen = ?', '0' );
				$select->where ( 'addressee = ?', $addressee );
				$msBird = $msData->fetchAll ( $select );
				foreach ( $msBird as $v ) {
					$v->readen = '1';
					$v->save ();
				}
				$msArray = $msBird->toArray ();
				$this->view->data = $msArray;
				break;
		}
	}
}